package com.his.dictcode.service.Imp;

import com.his.common.util.Utils;
import com.his.dictcode.dao.DictCodeMapper;
import com.his.dictcode.dao.DictCodeSonMapper;
import com.his.dictcode.entity.DictCode;
import com.his.dictcode.entity.DictCodeReturn;
import com.his.dictcode.entity.DictCodeSon;
import com.his.dictcode.service.DictCodeService;
import com.his.dictcode.vo.CacheCodeVo;
import com.his.dictcode.vo.DictCodeSonVo;
import com.his.dictcode.vo.DictCodeVo;
import com.his.dictcode.vo.DetailVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.exception.HisExecption;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.PinYin4jUtil;
import com.xiao.common.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author zhoufei
 * @Date 2020/1/21
 */
@Service
public class DictCodeServiceImpl implements DictCodeService
{
    @Autowired
    private  DictCodeMapper dictCodeMapper;
    @Resource
    private DictCodeSonMapper dictCodeSonMapper;
    @Resource
    private RedisUtils redisUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(DictCodeVo dictCodeVo) {
        DictCode dictCode = BeanUtil.copyBean(dictCodeVo,DictCode.class);
        List<DictCode> list = dictCodeMapper.selectByPrkey(dictCode);
        if(list.size()>0){
            throw HisExecption.serverErrException("基础编码已存在！");
        }
        dictCode.setINPUTSTR(PinYin4jUtil.converterToFirstSpell(dictCodeVo.getCODENAME()));
        dictCode.setSYSFLAG(0);
        dictCodeMapper.insertAllColumn(dictCode);
        this.saveDictSon(dictCodeVo);
    }
    @Override
    public void deleteByKey(DictCodeVo dictCodeVo) {
        DictCode dictCode = BeanUtil.copyBean(dictCodeVo,DictCode.class);
        dictCodeMapper.deleteById(dictCode);
    }
    @Override
    public List<DictCode> queryAll(DictCodeVo dictCodeVo){
        DictCode dictCode = BeanUtil.copyBean(dictCodeVo,DictCode.class);
        List<DictCode> dictCodes = dictCodeMapper.selectByEntity(dictCode);
        return dictCodes;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DictCodeVo dictCodeVo){
        DictCode dictCode = BeanUtil.copyBean(dictCodeVo,DictCode.class);
        dictCode.setINPUTSTR(PinYin4jUtil.converterToFirstSpell(dictCodeVo.getCODENAME()));
        dictCodeMapper.updateById(dictCode);
        this.saveDictSon(dictCodeVo);
    }

    @Override
    public void cacheCode() {
        DictCode dictCode = new DictCode();
        List<DictCode> dictCodeList = dictCodeMapper.selectByEntity(dictCode);
        redisUtils.set("dict_code", dictCodeList);
    }

    @Override
    public List<DictCode> getCacheCode(CacheCodeVo cacheCodeVo) {
        if (StringUtils.isEmpty(cacheCodeVo.getCODETYPEID())) {
            return null;
        }
        DictCode dictCode = new DictCode();
        dictCode.setCODETYPEID(cacheCodeVo.getCODETYPEID());
        if (StringUtils.isEmpty(cacheCodeVo.getCODEID())) {
            return dictCodeMapper.selectByEntity(dictCode);
        } else {
            dictCode.setCODEID(cacheCodeVo.getCODEID());
            return dictCodeMapper.selectByEntity(dictCode);
        }
    }

    @Override
    public List<DictCode> getCodeByTypeId(DictEmployee dictEmployee, String codeTypeId) {
        Map params = new HashMap<>();
        params.put("hospId",dictEmployee.getHOSPID());
        params.put("codeTypeId",codeTypeId);
        return dictCodeMapper.selectCodeByTpyeId(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDictSon(DictCodeVo dictCodeVo) {
       if(null != dictCodeVo.getCodeSonVoList() && dictCodeVo.getCodeSonVoList().size()>0) {
            List<DictCodeSonVo> codeSonVoList = dictCodeVo.getCodeSonVoList();
            if (null != codeSonVoList && codeSonVoList.size() > 0) {
                DictCodeSonVo dictCodeSonVo = codeSonVoList.get(0);
                DictCodeSon dictCodeSon = new DictCodeSon();
                dictCodeSon.setCodeid(dictCodeVo.getCODEID());
                dictCodeSon.setCodetypeid(dictCodeVo.getCODETYPEID());
                dictCodeSonMapper.deleteByPrimaryKey(dictCodeSon);
                for (DictCodeSonVo dictCodeSonVo1 : codeSonVoList) {
                    DictCodeSon dictCodeSonNew = BeanUtil.copyBean(dictCodeSonVo1, DictCodeSon.class);
                    dictCodeSonNew.setCodetypeid(dictCodeVo.getCODETYPEID());
                    dictCodeSonNew.setCodeid(dictCodeVo.getCODEID());
                    dictCodeSonMapper.insert(dictCodeSonNew);
                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> getCodeTree(String codeTypeId) {
        DictCode dictCode = new DictCode();
        dictCode.setCODETYPEID(codeTypeId);
        List<DictCode> list = dictCodeMapper.selectByEntity(dictCode);
        List<Map<String, Object>> codeList = new ArrayList<>();
        if(list.size()>0){
            //一级菜单
            for(DictCode dictCode1 : list){
                codeList.add(toNode(dictCode1));
            }
            //list转成tree
            return Utils.getTree(codeList,null,"id");
        }
        return null;
    }

    @Override
    public DictCodeReturn getCodeDetail(DetailVo detailVo) {
        DictCode dictCode = BeanUtil.copyBean(detailVo,DictCode.class);
        List<DictCode> list = dictCodeMapper.selectByPrkey(dictCode);
        if(list.size()>0) {
            DictCodeReturn dictCodeReturn = BeanUtil.copyBean(list.get(0),DictCodeReturn.class);
            DictCodeSon dictCodeSon = new DictCodeSon();
            dictCodeSon.setCodeid(dictCode.getCODEID());
            dictCodeSon.setCodetypeid(dictCode.getCODETYPEID());
            List<DictCodeSon> sonList = dictCodeSonMapper.selectByPrimaryKey(dictCodeSon);
            dictCodeReturn.setCodeSonVoList(sonList);
            return dictCodeReturn;
        }
        return null;
    }

    private Map<String ,Object> toNode(DictCode dictCode){
        Map<String, Object> node = new HashMap<>();
        node.put("id",dictCode.getCODEID());
        node.put("label",dictCode.getCODENAME());
        node.put("codeTypeId",dictCode.getCODETYPEID());
        node.put("pid", dictCode.getUPCODE());
        node.put("grade",dictCode.getGRADE());
        return node;
    }

}
